/**
 * 作者:hanbo
 * 日期:2018/11/2116:46
 * 描述:点类型
 */
import {EXTENDS_3857} from "../core/Constant";
import {Geometry} from "./Geometry";
import {BoundingBox} from "./BoundingBox";
import {PointStyle} from "./style/PointStyle";

/**
 * 点样式pointSize:像素大小;r:像素大小g:像素大小
 */
export class Point extends Geometry {
    constructor(x = 0, y = 0) {
        super("point", new BoundingBox([x, y]));
        this.x = x;
        this.y = y;

    }

    /**
     * OpenGL绘图坐标系转到3857坐标系
     * @param point {Point}  OpenGL绘图坐标系点
     * @returns {Point}
     */
    static glToMector(point) {
        return new Point(point.x * EXTENDS_3857, point.y * EXTENDS_3857)
    }

    static mectorToWGS(point) {
        let radio = 180 / EXTENDS_3857;
        let x = point.x * radio;
        let y = point.y * radio;
        y = 180 / Math.PI * (2 * Math.atan(Math.exp(y * Math.PI / 180)) - Math.PI / 2);
        return new Point(x, y);
    }

    /**
     * 获取两点距离
     * @param point1 {Point} 点1
     * @param point2 {Point} 点2
     */
    static getDistance(point1, point2) {
        return Math.sqrt(Math.pow(point1.x - point2.x, 2) + Math.pow(point1.y - point2.y, 2))
    }

    /**
     * 将点数组转换为坐标数组
     * @param array {Array} Point类型点数组
     * @returns {Array} 结果坐标数组
     */
    static pointsToArray(array) {
        let result = [];
        let point;
        for (let i = 0; i < array.length; i++) {
            point = array[i];
            result.push(point.x, point.y)
        }
        return result;
    }

    getStyle() {
        if (this.style) {
            return this.style
        } else {
            return new PointStyle()
        }
    }

    /**
     * 获取随机点
     * @returns {Point} 随机点
     */
    static randomPoint(){
        let numX=Math.random()*2-1;//-1到1随机数
        let numY=Math.random()*2-1;//-1到1随机数
         return new Point(EXTENDS_3857*numX,EXTENDS_3857*numY);
    }
}